package com.a16.yuejian.yuejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.MyFollowActivity;
import com.a16.yuejian.yuejian.javabean.Follow;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.ClubModel;
import com.a16.yuejian.yuejian.model.MasterModel;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 王子恒 on 2017/11/11.
 */

public class MyFollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Button my_follow_button;
    private MyFollowActivity mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public MyFollowAdapter(MyFollowActivity mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    SuccessListener listListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){

            }else if (success.getSuccess().equals("0")){
                mContext.getData();
            }
        }

        @Override
        public void onFail() {

        }
    };


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_followcardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Follow entity = (Follow) mDataList.get(position);
        if (null == entity){
            return;
        }else {
            final ViewHolder viewHolder = (ViewHolder) holder;
           if (entity.getType().equals("3")){
               viewHolder.my_follow_name.setText(entity.getCoachname());
               Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+entity.getCoachpic()).into(viewHolder.my_follow_userpic);
               viewHolder.my_follow_button.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       MasterModel masterModel = new MasterModel();
                       masterModel.follow(entity.getCoachid(),entity.getUserid(),"3",listListener);
                   }
               });
           }else if (entity.getType().equals("5")) {
               viewHolder.my_follow_name.setText(entity.getClubname());
               Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/" + entity.getClubpic()).into(viewHolder.my_follow_userpic);
               viewHolder.my_follow_button.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       ClubModel clubModel = new ClubModel();
                       clubModel.follow(entity.getClubid(),entity.getUserid(),"5",listListener);
                   }
               });
           }

        }

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView my_follow_userpic;
        TextView my_follow_name;
        Button my_follow_button;
        LinearLayout my_follow_container;

        public ViewHolder(View itemView) {
            super(itemView);
            my_follow_name = (TextView) itemView.findViewById(R.id.my_follow_name);
            my_follow_userpic = (ImageView) itemView.findViewById(R.id.my_follow_userpic);
            my_follow_button = (Button)itemView.findViewById(R.id.my_follow_button);
            my_follow_container= (LinearLayout)itemView.findViewById(R.id.my_follow_container);
        }
    }


}
